//
//  HERealNameAuthAlterController.m
//  HealthElderly
//
//  Created by 吴欧 on 2020/11/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HERealNameAuthAlterController.h"
#import "HEFaceAuthManage.h"
#import "HEOcrManage.h"
#import "HERNAuthViewController.h"


@interface HERealNameAuthAlterController ()<HEFaceAuthManageDelegate, HEOcrManageDelegate>

@property (strong, nonatomic) UIView *protocal;
//@property (strong, nonatomic) WBIDCardInfoModel *model;

@end

@implementation HERealNameAuthAlterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    
    [self initUI];
    [self show];
}

- (void)initUI {
    
    CGFloat headHeight = 48;

    // 初始化隐私政策
    _protocal =[[UIView alloc] init];
    UIColor *color = [UIColor blackColor];
    _protocal.backgroundColor = [color colorWithAlphaComponent:0.3];
    [self.view addSubview:_protocal];
    _protocal.sd_layout
    .topEqualToView(self.view)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view);
    
    // 内容父视图
    UIImageView *contentView =[[UIImageView alloc] init];
    contentView.image = IMAGE_NAMED(@"auth_bg");
    contentView.userInteractionEnabled = YES;
    [_protocal addSubview:contentView];
    contentView.sd_layout
    .widthIs(298)
    .heightIs(240)
    .centerXEqualToView(_protocal)
    .centerYEqualToView(_protocal);
    
    // 头部视图
    UILabel *headView =[[UILabel alloc] init];
    headView.text = @"您还没有实名认证";
    headView.textAlignment = NSTextAlignmentCenter;
    headView.font = UIFontBoldMake(20);
    [contentView addSubview:headView];
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).mas_offset(90);
        make.right.left.equalTo(contentView);
    }];
    
    UILabel *noticLb =[[UILabel alloc] init];
    noticLb.text = @"请前往实名认证，完善个人信息";
    noticLb.textAlignment = NSTextAlignmentCenter;
    noticLb.font = UIFontMake(14);
    noticLb.textColor = CNavSubTitleColor;
    [contentView addSubview:noticLb];
    
    [noticLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.mas_bottom).mas_offset(10);
        make.right.left.equalTo(contentView);
    }];
     
    // 中间视图
    YYLabel *middle =[[YYLabel alloc] init];
    middle.textVerticalAlignment = YYTextVerticalAlignmentTop;
    middle.textAlignment = NSTextAlignmentLeft;
    [contentView addSubview:middle];
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:16], NSForegroundColorAttributeName: [UIColor blackColor]};

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"请您务必审慎阅读《实名认证协议》" attributes:attributes];
    

    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:@"《实名认证协议》"] color:CNavBgColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [self toSeeProtcal:@"https://www.honglutec.com/agreement/auth.html"];
    }];
 
    // 居中
    NSMutableParagraphStyle *timeParagraphStyle = [[NSMutableParagraphStyle alloc] init];
    timeParagraphStyle.alignment = NSTextAlignmentCenter;
    [text addAttribute:NSParagraphStyleAttributeName value:timeParagraphStyle range:NSMakeRange(0, text.length)];
    
    middle.numberOfLines = 0;
    middle.attributedText = text;
     
    [middle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noticLb.mas_bottom).mas_offset(5);
        make.right.left.equalTo(contentView);
    }];
    

    // 底部按钮
    UIView *btnWarp = [UIView new];
    [contentView addSubview:btnWarp];
    
    [btnWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(contentView.mas_bottom).mas_offset(-20);
        make.right.equalTo(contentView).mas_offset(-15);
        make.left.equalTo(contentView).mas_offset(15);
        make.height.mas_offset(34);
    }];

    QMUIGhostButton *refuseBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
    refuseBtn.titleLabel.font = UIFontMake(16);
    [refuseBtn setTitle:@"暂不认证" forState:UIControlStateNormal];
    [btnWarp addSubview:refuseBtn];

    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = UIFontMake(16);
    [agreeBtn setTitle:@"立即认证" forState:UIControlStateNormal];
    [btnWarp addSubview:agreeBtn];
    
    [refuseBtn addTarget:self action:@selector(refuse:) forControlEvents:UIControlEventTouchUpInside];
    [agreeBtn addTarget:self action:@selector(agreen:) forControlEvents:UIControlEventTouchUpInside];

    [refuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btnWarp);
        make.left.equalTo(btnWarp).mas_offset(10);
        make.width.mas_offset(110);
        make.height.mas_offset(34);
    }];
    
    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btnWarp);
        make.right.equalTo(btnWarp).mas_offset(-10);
        make.width.mas_offset(110);
        make.height.mas_offset(34);
    }];
 
}


- (void)toSeeProtcal:(NSString *)protcalName {
//    NSString * fileUrl = [[NSBundle mainBundle]pathForResource:protcalName ofType:@"html"];
//    NSURL * file = [NSURL fileURLWithPath:fileUrl];
    
    NSURL *file = [NSURL URLWithString:protcalName];

//    SFSafariViewController *safariVc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"https://blog.csdn.net/weixin_30478923/article/details/99062594"]];
//    [self presentViewController:safariVc animated:YES completion:nil];

    
    RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
    webView.isShowCloseBtn = YES;
    webView.showPageTitle = YES;
    RootNavigationController *nav = [[RootNavigationController alloc] initWithRootViewController:webView];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
//    nav.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

//    [webView.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
//    [webView.navigationController.navigationBar setTintColor:[UIColor blackColor]];
//    [webView.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:18]}];
    
     [self presentViewController:nav animated:YES completion:nil];
}

- (void)show {
    __weak typeof(self) weakSelf = self;
    weakSelf.protocal.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.protocal.alpha = 1;
     }];
}

- (void)hide {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3  animations:^{
        weakSelf.protocal.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf.view removeFromSuperview];
    }];
}

- (void)refuse:(id)sender {
    if ([_delegate respondsToSelector:@selector(realnameAuthCancelAction)]) {
        [_delegate realnameAuthCancelAction];
    }
    [self hide];
}

- (void)agreen:(id)sender {
    // 扫码用户身份证正反面
//    [HEOcrManage sharedClient].delegate = self;
//    [[HEOcrManage sharedClient] startServiceWithType:WBOCRSDKTypeIDCardNormal];
    

    HERNAuthViewController *vc = [HERNAuthViewController new];
    vc.backBlock = ^{
        [self hide];
    };
    HEBaseNavigationController *nav = [[HEBaseNavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - HEFaceAuthManageDelegate
//- (void)faceAuthResults:(BOOL)isSucces {
//
//    NSMutableDictionary *params = @{}.mutableCopy;
//    [params setValue:_model.name forKey:@"realname"];
//    [params setValue:_model.authority forKey:@"signingOrganization"];
//    [params setValue:_model.address forKey:@"address"];
//    [params setValue:_model.sex forKey:@"sex"];
//    [params setValue:_model.idcard forKey:@"idCardNumber"];
//    [params setValue:_model.nation forKey:@"national"];
//    [params setValue:_model.validDate forKey:@"validTerm"];
//    [params setValue:_model.birth forKey:@"birthday"];
//
//
//    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KVerifyUser parameters: params success:^(id  _Nonnull responseObject) {
//        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
//        if (response.success) {
//            [Dialog toastBottom:@"实名认证成功"];
//            UserInfo *data = [UserInfo mj_objectWithKeyValues:response.data];
//            data.qrcode_url = response.data[@"qrCode_url"];
//            // 存储用户数据
//            [[UserManager sharedUserManager] saveUserInfo:data];
//
//            [self hide];
//
//            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationRealAuth object:nil];
//            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationUserInfoChange object:nil];
//        }
//
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
//}
//
//#pragma mark - HEOcrManageDelegate
//- (void)ocrResult:(id)res {
//
//    WBIDCardInfoModel *model = (WBIDCardInfoModel *)res;
//    _model = model;
//
//    // 发起人脸核身
//    [HEFaceAuthManage sharedClient].name = model.name;
//    [HEFaceAuthManage sharedClient].idNo = model.idcard;
//    [HEFaceAuthManage sharedClient].delegate = self;
//    [[HEFaceAuthManage sharedClient] startServiceWithType:WBFaceVerifyLivingType_Action];
//}

- (void)ocrCancel {
    [self hide];
}

@end
